import createEnhanceAxiosInstance from '../src/index.js'
import qs from 'qs'

// 创建请求实例
const $fetch = createEnhanceAxiosInstance({
  axiosInitConfig: {
    baseURL: 'http://dev.bs.com',
    // timeout: 10000,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    responseType: 'json',
    withCredentials: true,
    transformRequest: [
      function (data) {
        //提交前的数据处理
        return qs.stringify(data)
      }
    ],
    transformResponse: [
      function (data) {
        //请求返回后的数据处理
        return data
      }
    ]
  },
  requestInterceptors: config => {
    if(window.xhprof == 1) {//后端触发性能分析
      if(config.params) {
        config.params = {
          ...config.params,
          xhprof: 1,
        }
      } else {
        config.params = {
          xhprof: 1,
        }
      }
    }

    if(window.sqldebug == 1) {//后端触发SQL分析
      if(config.params) {
        config.params = {
          ...config.params,
          sqldebug: 1,
        }
      } else {
        config.params = {
          sqldebug: 1,
        }
      }
    }

    return config
  },
  responseInterceptors: response => {
    if (response.data && response.data.status === '401') {
      if (store.state.user_info.id) {
        store.commit('setRelogin', true)
      } else {
        router.replace({
          path: '/'
        }) //登陆成功后跳入当前页面
      }
      localStorage.removeItem('login_permiss')
      if (response.data) {
        response.data.status = false
      }
    }
    // 外检服务器逻辑
    if (response.data && response.data.status === '402') {
      router.push({name: 'EimInfoImport_outer'})
      if (response.data) {
        response.data.status = false
      }
    }
    // 系统是否正在维护
    if (response.data && response.data.data && response.data.data.param_value === 'Y') {
      let maintain_route = localStorage.getItem('maintain_route')
      if (!maintain_route) {
        let maintain_route = {}
        maintain_route.path = router.history.current.path
        maintain_route.query = router.history.current.query
        localStorage.setItem('maintain_route', JSON.stringify(maintain_route))
      }
      router.push({
        name: 'systemMaintain'
      })
    }
    return response
  },
  cancelWhiteList: [
    '/manager/api/commondata/version',
    '/manager/api/Commonconfig/get_parameter',
  ],
  cacheList: [

  ]
})